package com.fjwt.gz.mgr.ctrl.test;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.util.CharsetUtil;
import com.fjwt.gz.core.model.ApiRes;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/api/redis")
public class GetRedisController {

    /**导出redis数据**/
   /* @PostMapping("/getFile")
    public ApiRes getFile(){
        Jedis jedis = new Jedis("192.200.0.36",11001);
         jedis.auth("Redis@2025");
        jedis.select(6);
        Set<String> keys = jedis.keys("*");
        File file = new File("./8888.txt");
        log.info("路径地址：{}",file.getAbsolutePath());
        FileWriter fileWriter=new FileWriter(file, CharsetUtil.CHARSET_UTF_8);

        List list=new ArrayList();
        for (String key:keys) {
            Long ttl = jedis.ttl(key);
            String value = jedis.get(key);
            list.add(key+"@#"+value+"@#"+String.valueOf(ttl));
        }
        fileWriter.writeLines(list);
        log.info("导出成功");
        return ApiRes.ok();
    }*/

   /* public static void main(String[] args) {
        Jedis jedis = new Jedis("192.168.1.185");
        jedis.auth("123456");
        jedis.select(6);
        Set<String> keys = jedis.keys("*");
       FileWriter fileWriter=new FileWriter("C:\\Users\\chang\\Desktop\\1002\\8888.txt", CharsetUtil.CHARSET_UTF_8);

        List list=new ArrayList();
        for (String key:keys) {
            Long ttl = jedis.ttl(key);
            String value = jedis.get(key);
            list.add(key+"@#"+value+"@#"+String.valueOf(ttl));
        }
        fileWriter.writeLines(list);
    }*/


   /* public static void main(String[] args) {

        Jedis jedis = new Jedis("192.168.1.185");
        jedis.auth("123456");
        jedis.select(7);
        BufferedReader reader = FileUtil.getReader("C:\\Users\\chang\\Desktop\\1002\\8888.txt", CharsetUtil.CHARSET_UTF_8);
        try {
            while (true) {
                String s = reader.readLine();
                System.out.println(s);
                if (StringUtils.isEmpty(s)){
                    break;
                }
                String[] split = s.split("@#");
                jedis.set(split[0],split[1]);
                Long ttl = Long.valueOf(split[2]);
                if(ttl > 0){
                    jedis.expire(split[0],Long.valueOf(split[2]));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }*/

    /**导入redis数据**/
    /*@PostMapping("/upFile")
    public ApiRes upFile(){
        Jedis jedis = new Jedis("192.200.0.36", 11002);
        jedis.auth("Redis@2025");
        jedis.select(6);
//        BufferedReader reader = FileUtil.getReader("D:\\workspace\\province-marketing\\java\\8888.txt", CharsetUtil.CHARSET_UTF_8);
         BufferedReader reader = FileUtil.getReader("/home/jrstyx/marketing/manager/8888.txt", CharsetUtil.CHARSET_UTF_8);
        try {
            while (true) {
                String s = reader.readLine();
                System.out.println(s);
                if (StringUtils.isEmpty(s)){
                    break;
                }
                String[] split = s.split("@#");
                jedis.set(split[0],split[1]);
                Long ttl = Long.valueOf(split[2]);
                if(ttl > 0){
                    jedis.expire(split[0],Long.valueOf(split[2]));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return ApiRes.ok();
    }*/
}
